package com.upu173.music.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andbase.library.asynctask.AbTask;
import com.andbase.library.asynctask.AbTaskItem;
import com.andbase.library.asynctask.AbTaskObjectListener;
import com.andbase.library.cache.AbImageCache;
import com.andbase.library.view.recycler.AbRecyclerViewAdapter;
import com.upu173.music.R;
import com.upu173.music.activity.MusicListActivity;
import com.upu173.music.model.Song;
import com.upu173.music.model.SongTag;
import com.upu173.music.utils.MusicUtil;

import java.util.List;

public class SongRecyclerViewAdapter extends AbRecyclerViewAdapter {

    private MusicListActivity activity;
    private List<Song> songList;
    private AbImageCache cache;

    public SongRecyclerViewAdapter(Context context, List<Song> items) {
        super(context,items);
        this.activity = (MusicListActivity)context;
        this.songList = items;
        cache = AbImageCache.getInstance(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateRecyclerViewHolder(ViewGroup parent, int viewType) {
        //必须这么写，不能用View.inflate
        View view = LayoutInflater.from(activity).inflate(R.layout.music_item_song_list,parent,false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindRecyclerViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder)holder;
        final Song song = songList.get(position);
        recyclerViewHolder.nameTextView.setText(song.getName());
        recyclerViewHolder.userTextView.setText(song.getSinger()==null ? "":song.getSinger());
        recyclerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.onSongClick(position);

            }
        });

        if(cache.containKey(song.getPath())){
            Bitmap bitmap =  cache.getBitmap(song.getPath());
            if(bitmap!= null){
                recyclerViewHolder.thumbImage.setImageBitmap(bitmap);
            }else{
                recyclerViewHolder.thumbImage.setImageResource(R.drawable.ic_music_file);
            }
        }else{
            AbTask task = AbTask.newInstance();
            AbTaskItem item = new AbTaskItem();
            item.setListener(new AbTaskObjectListener<SongTag>() {
                @Override
                public SongTag getObject() {
                    return MusicUtil.getSongTag(activity,song.getPath(),true);
                }

                @Override
                public void update(SongTag songTag) {
                    cache.putBitmap(song.getPath(),songTag.getBitmap());
                    if(songTag.getBitmap()!= null){
                        recyclerViewHolder.thumbImage.setImageBitmap(songTag.getBitmap());
                    }else{
                        recyclerViewHolder.thumbImage.setImageResource(R.drawable.ic_music_file);
                    }
                }
            });
            task.execute(item);
        }

        recyclerViewHolder.addImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.addSongClick(position);

            }
        });
    }


    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public final ImageView imageView;
        public final TextView nameTextView;
        public final TextView userTextView;
        public final ImageView thumbImage;
        public final ImageView addImageView;
        public final View itemView;

        public RecyclerViewHolder(View view) {
            super(view);
            itemView = view;
            imageView = (ImageView) view.findViewById(R.id.item_image);
            nameTextView = (TextView) view.findViewById(R.id.item_name);
            userTextView = (TextView) view.findViewById(R.id.item_user);
            thumbImage = (ImageView) view.findViewById(R.id.item_image);
            addImageView = (ImageView) view.findViewById(R.id.item_add);
        }
    }


}